//
//  HOTMemoRangeViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-22.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTBloodChooseViewController.h"
#import "HOTBloodChooseCell.h"

@interface HOTBloodChooseViewController () <HOTBloodChooseCellDelegate> {
    
    NSArray *_dataList;
    NSString *_selBloodId;
    NSDictionary *_selBloodInfo;
}

@end

@implementation HOTBloodChooseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil selBloodId:(NSString *)str
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _selBloodInfo = @{@"id":[HOTUtils getString:str]};
        _selBloodId = str;

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviTitle:@"血液型"];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_profile"]];
    
    [self addRightBtnTarget:self action:@selector(descideBtnClicked) image:[UIImage imageNamed:@"btn_decision_off"] hImage:[UIImage imageNamed:@"btn_decision_on"]];
    
    _dataList = @[@{@"id":@"1", @"name":@"A型"},@{@"id":@"2", @"name":@"B型"},
                  @{@"id":@"3", @"name":@"O型"},@{@"id":@"4", @"name":@"AB型"}];
}

#pragma mark - UItableView DataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"cellID";
    HOTBloodChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        UINib *nib = [UINib nibWithNibName:XIB(@"HOTBloodChooseCell") bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellId];
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    }
    
    cell.delegate = self;
    cell.indexPath = indexPath;
    NSDictionary *dic = [_dataList objectAtIndex:indexPath.row];
    NSString *bloodId = [dic objectForKey:@"id"];
    NSString *bloodname = [dic objectForKey:@"name"];
    cell.cateTitleLabel.text = bloodname;
    
    if ([[_selBloodInfo objectForKey:@"id"] isEqualToString:bloodId]) {
        cell.radioBtn.selected = YES;

        _selBloodInfo = dic;
    } else {
        cell.radioBtn.selected = NO;
    }
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataList.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 62;
}

#pragma mark - UITableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    _selBloodInfo = [_dataList objectAtIndex:indexPath.row];
    
    [tableView reloadData];
    //HOTBloodChooseCell *cell = (HOTBloodChooseCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    //cell.radioBtn.selected = YES;
}

-(void)radioBtnClicked:(BOOL)isChecked index:(NSIndexPath *)indexPath {
    
//    if (isChecked) {
//        _selBloodInfo = [_dataList objectAtIndex:indexPath.row];
//    }
    
    //[self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 私有函数
-(void)descideBtnClicked {
        
    NSString *bloodName = [_selBloodInfo objectForKey:@"name"];
    NSString *bloodId = [_selBloodInfo objectForKey:@"id"];
    [self.delegate chooseBloodId:bloodId bloodName:bloodName];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
